package cn.com.mdx.sort.heapsort.eventdrivensimulation;

import edu.princeton.cs.algs4.StdDraw;

/**
 * <p>
 *
 * @author ：huangy
 * @date ：Created in 2020/10/19 5:55 下午
 */
public class Ball {

    /**
     * 球的位置，rx：球x轴坐标 ry：球y轴坐标
     */
    private double rx = 0.01, ry = 0.01;

    /**
     * 球的初始速度，vx：球x方向速度 vy：球y方向速度
     */
    private double vx = 0.05, vy = 0.05;

    /**
     * 球的半径
     */
    private final double radius = 0.01;

    public Ball() {
    }

    public Ball(double rx, double ry) {
        this.rx = rx;
        this.ry = ry;
    }

    public Ball(double rx, double ry, double vx, double vy) {
        this.rx = rx;
        this.ry = ry;
        this.vx = vx;
        this.vy = vy;
    }

    public void move(double dt) {
        //检测球是否与墙碰撞
        if ((rx + vx * dt < radius) || (rx + vx * dt > 1.0 - radius)) vx = -vx;
        if ((ry + vy * dt < radius) || (ry + vy * dt > 1.0 - radius)) vy = -vy;
        //位置变更
        rx = rx + vx * dt;
        ry = ry + vy * dt;
    }

    public void draw() {
        StdDraw.filledCircle(rx, ry, radius);
    }
}
